package rtcf.framework
{
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.net.Responder;
	import rtcf.events.*;
	
	/**
	 * Something people can call rtcfComponent.methodName.addEventListener on.
	 * The RTCFComponent will handle the actual call?
	 * 
	 * 
	 */

	public class RemoteMethodProxy extends EventDispatcher
	{
		public var responder:Responder;
		
		public function RemoteMethodProxy(target:IEventDispatcher=null)
		{
			super(target);
			responder = new Responder(_handleResult, _handleStatus);
		}
		
		public override function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean=false):void{
			
			if (type != "result" && type != "fault"){
				throw("RemoteMethodProxy.addEventListener> Hell no, you can only listen for result or fault events!");
			}
						
			super.addEventListener(type, listener, useCapture, priority, useWeakReference);
		}
		
		private function _handleResult(result:Object):void{
			// dispatch result events to listeners that includes the result object within the event.
			//trace("_handleResult> " + result);
			dispatchEvent( new RMIResultEvent(result) );
		}
		
		private function _handleStatus(status:Object):void{
			// dispatch fault events to listeners that inlcudes the fault object within the fault event.
			//trace("_handleStatus> " + status);
			dispatchEvent( new RMIFaultEvent(status) );
		}
				
	}
}